<!DOCTYPE html>
<html>
<head>
    <title>登录 - CVV Web</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container mt-5">
        <div class="row justify-content-center">
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        <h3 class="text-center">用户登录</h3>
                    </div>
                    <div class="card-body">
                        <form id="loginForm">
                            <div class="mb-3">
                                <label for="identifier" class="form-label">用户名或邮箱</label>
                                <input type="text" class="form-control" id="identifier" name="identifier" required>
                            </div>
                            <div class="mb-3">
                                <label for="password" class="form-label">密码</label>
                                <input type="password" class="form-control" id="password" name="password" required>
                            </div>
                            <button type="submit" class="btn btn-primary w-100">登录</button>
                        </form>
                        <div id="message" class="mt-3"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        document.getElementById('loginForm').addEventListener('submit', async function(e) {
            e.preventDefault();

            const formData = {
                identifier: document.getElementById('identifier').value,
                password: document.getElementById('password').value
            };

            try {
                const response = await fetch('/admin/auth/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(formData)
                });

                const result = await response.json();

                if (result.success) {
                    // 存储token
                    localStorage.setItem('token', result.token);
                    // 跳转到主页
                    window.location.href = '/';
                } else {
                    document.getElementById('message').innerHTML = `
                        <div class="alert alert-danger">${result.message}</div>
                    `;
                }
            } catch (error) {
                document.getElementById('message').innerHTML = `
                    <div class="alert alert-danger">登录失败: ${error.message}</div>
                `;
            }
        });
    </script>
</body>
</html>